package com.ztr.test.ip;

import java.net.InetAddress;
import java.net.NetworkInterface;  
import java.net.SocketException;  
import java.util.Enumeration;  
  
public class LocalIPAddresses {  
    public static void main(String[] args) {  
        try {
            // 获取本地机器的所有网络接口的IP地址
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();  
            while (interfaces.hasMoreElements()) {  
                NetworkInterface inf = interfaces.nextElement();
                for (Enumeration<InetAddress> adders = inf.getInetAddresses(); adders.hasMoreElements();) {
                    InetAddress addr = adders.nextElement();
                    System.out.println("接口名称: " + inf.getName() + " IP地址: " + addr.getHostAddress());
                }  
            }  
        } catch (SocketException e) {  
            e.printStackTrace();  
        }  
    }  
}